package BookFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BookLoginFrame extends JFrame {

    private JButton loginButton = new JButton("登录");
    private JButton resetButton = new JButton("重置");
    private JTextField accountField = new JTextField(10);
    private JPasswordField passwordField = new JPasswordField(10);

    // 声明屏幕尺寸和窗口尺寸
    private int windowWidth;
    private int windowHeight;
    private int screenWidth;
    private int screenHeight;

    public BookLoginFrame() {
        setSize(300, 180); // 设置窗口大小
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        screenWidth = (int) dimension.getWidth();
        screenHeight = (int) dimension.getHeight();
        windowWidth = getWidth();
        windowHeight = getHeight();
        // 窗口居中
        setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
        setTitle("图书管理系统登录"); // 修改为图书管理主题
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout()); // 流式布局

        // 账号输入面板
        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("管理员账号："));
        panel1.add(accountField);
        getContentPane().add(panel1);

        // 密码输入面板
        JPanel panel2 = new JPanel();
        panel2.add(new JLabel("登录密码："));
        panel2.add(passwordField);
        getContentPane().add(panel2);

        // 按钮面板
        JPanel panel3 = new JPanel();
        panel3.add(loginButton);
        panel3.add(resetButton);
        getContentPane().add(panel3);

        // 登录事件处理
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String account = accountField.getText().trim();
                String password = new String(passwordField.getPassword()).trim();

                // 简单验证（实际需对接数据库）
                if ("admin".equals(account) && "admin123".equals(password)) { // 修改默认密码增强安全性
                    // 跳转至图书管理主界面（JTableBooks）
                    new JTableBooks(BookLoginFrame.this);
                    // 隐藏当前登录窗口
                    setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(null, "账号或密码错误，请重新输入！",
                            "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 重置事件处理
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                accountField.setText("");
                passwordField.setText("");
            }
        });
    }

    public static void main(String[] args) {
        new BookLoginFrame().setVisible(true); // 启动登录界面
    }
}
